import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class HelloWorld {
    public static void main(String[] args) {
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=图书馆数据库;encrypt=true;trustServerCertificate=true";
        String userName = "sa";
        String userPwd = "147258";
        Connection con;
        Statement stmt;
        ResultSet rs;
        String sqlStr = "select *from student";
        try {
            Class.forName(driverName);
            System.out.println("类实例化成功！");
            con = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("创建连接对象成功！");
            stmt = con.createStatement();
            System.out.println("创建Statement成功！");
            rs = stmt.executeQuery(sqlStr);
            System.out.println("操作数据表成功！");
            System.out.println("___________________________");
            System.out.println("编号     姓名          地址               出生日期");
            while (rs.next()) {
                System.out.print(rs.getString("编号").trim() + "      ");
                System.out.print(rs.getString("姓名").trim() + "         ");
                System.out.print(rs.getString("地址").trim()+"          ");
                System.out.print(rs.getString("出生日期").trim());
                System.out.println("\r");
            }
            rs.close();
            stmt.close();
            con.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
